package com.bci.pwtz.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.bci.pwtz.common.util.PaginationList;
import com.bci.pwtz.common.util.PwtzUtils;
import com.bci.pwtz.exception.CanNotDeleteException;
import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.mysql.dao.DysydlbhqMapper;
import com.bci.pwtz.mysql.model.Dysydlbhq;
import com.bci.pwtz.service.BusinessRefService;
import com.bci.pwtz.service.CompanyService;
import com.bci.pwtz.service.DysydlbhqService;

/**
 * 一句话描述 <br/>
 * <p>
 * Description:<br/>
 * <p>
 * TODO 详细描述
 * 
 * <p>
 * For Examples <br/>
 * 
 * <PRE>
 *      TODO 代码使用示例
 * </PRE>
 * <p>
 */
@Service
public class DysydlbhqServiceImpl implements DysydlbhqService
{
    private DysydlbhqMapper objMapper;
    
    private CompanyService companyService;
    
    private BusinessRefService businessRefService;
    
    private static final Logger _logger = Logger.getLogger(PdbyqServiceImpl.class);

    /**
     * @return the objMapper
     */
    public DysydlbhqMapper getObjMapper()
    {
        return objMapper;
    }

    /**
     * @param objMapper the objMapper to set
     */
    public void setObjMapper(DysydlbhqMapper objMapper)
    {
        this.objMapper = objMapper;
    }

    /**
     * @return the companyService
     */
    public CompanyService getCompanyService()
    {
        return companyService;
    }

    /**
     * @param companyService the companyService to set
     */
    public void setCompanyService(CompanyService companyService)
    {
        this.companyService = companyService;
    }

   

    public BusinessRefService getBusinessRefService()
    {
        return businessRefService;
    }

    public void setBusinessRefService(BusinessRefService businessRefService)
    {
        this.businessRefService = businessRefService;
    }

    @Override
    public Dysydlbhq add(Dysydlbhq dysydlbhq) throws PwtzDatabaseException
    {
        if (_logger.isDebugEnabled())
        {
            _logger.debug("add:dysydlbhq=" + dysydlbhq.toString());
        }
        if (dysydlbhq == null)
        {
            if (_logger.isDebugEnabled())
            {
                _logger.debug("add,dysydlbhq is null");
            }
            return null;
        }else{
//            UUID uuid = UUID.randomUUID();
//            dysydlbhq.setDeviceid(uuid.toString());
            dysydlbhq.setStatus(1);
            dysydlbhq.setLastmodifiedtime(new Date());
            dysydlbhq.setCreatedate(new Date());
        }
        try
        {
        	objMapper.insertSelective(dysydlbhq);
        }
        catch (DataAccessException _e)
        {
            _logger.error(ExceptionUtils.getStackTrace(_e));
            throw new PwtzDatabaseException();
        }
        return dysydlbhq;
    }

    @Override
    public Dysydlbhq load(String dysydlbhqId) throws PwtzDatabaseException
    {
        if (_logger.isInfoEnabled())
        {
            _logger.info("load:dysydlbhqId=" + dysydlbhqId);
        }
        Dysydlbhq dysydlbhq = null;
        try
        {
        	dysydlbhq = objMapper.selectByPrimaryKey(dysydlbhqId);
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        if (dysydlbhq != null)
        {
            _logger.debug("load:result dysydlbhq=" + dysydlbhq.toString());
        }
        return dysydlbhq;
    }

    @Override
    public int delete(String dysydlbhqId) throws PwtzDatabaseException, CanNotDeleteException
    {
        int result=0;
        if (_logger.isDebugEnabled())
        {
            _logger.debug("delete:dysydlbhqId=" + dysydlbhqId);
        }
        try
        {
            boolean bl = businessRefService.canDelete("T_DYSYDLBHQ", dysydlbhqId);
            if(!bl){
                throw new CanNotDeleteException();
            }
            result = objMapper.deleteById(dysydlbhqId,new Date());
        }
        catch (DataAccessException _e)
        {
            _logger.error(ExceptionUtils.getStackTrace(_e));
            throw new PwtzDatabaseException();
        }
        return result;
    }

    @Override
    public int update(Dysydlbhq dysydlbhq) throws PwtzDatabaseException
    {
        int result=0;
        if (_logger.isDebugEnabled())
        {
            _logger.debug("update:dysydlbhq=" + dysydlbhq.toString());
        }
        if (dysydlbhq == null)
        {
            if (_logger.isDebugEnabled())
            {
                _logger.debug("update,dysydlbhq is null");
            }
            return 0;
        }else{
            dysydlbhq.setLastmodifiedtime(new Date());
        }
        try
        {
            result = objMapper.updateByPrimaryKeySelective(dysydlbhq);
        }
        catch (DataAccessException _e)
        {
            _logger.error(ExceptionUtils.getStackTrace(_e));
            throw new PwtzDatabaseException();
        }
        return result;
    }

    @Override
    public PaginationList<Dysydlbhq> findDysydlbhq(String devicename,Long companyid,int portalType,String[] medLineCodes,String lineCode,String beginTime, String endTime, int pageSize, int pageNo) throws PwtzDatabaseException
    {
        PaginationList<Dysydlbhq> paginationList = new PaginationList<Dysydlbhq>();
        
        if (_logger.isInfoEnabled())
        {
            _logger.info("findDybx:|devicename=" + devicename+"|beginTime=" + beginTime+"|endTime=" + endTime+"|companyid="+companyid+"|portalType="+portalType+"|medLineCodes="+PwtzUtils.parseCharacterArrayToString(medLineCodes)+"|lineCode="+lineCode+"|pageSize="+pageSize+"|pageNo="+pageNo);
        }
        List<Dysydlbhq> list = null;
        int benginIndex = pageSize * pageNo - pageSize;
        String companyidStr = "";
        String[] companyids=null;
        if(portalType==1){
            companyidStr = companyService.findChildrensId(companyid,null);
            if(companyidStr!=null && companyidStr.length()>0){
                companyidStr= companyidStr.substring(0,companyidStr.length()-1);
                companyids =companyidStr.split(",");
            }
        }
        System.out.println(devicename+"|"+companyid+"|"+companyids);
        try
        {
            list = objMapper.selectDeviceList(devicename,companyid,companyids,portalType,medLineCodes,lineCode,beginTime,endTime,benginIndex, pageSize);
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        if (list!=null)
        {
            _logger.debug("findDysydlbhq:result menu=" + list.toString());
        }
        int totalCount = 0;
        int totalPage =1;
        if(pageSize!=0){
            try
            {
                totalCount = objMapper.selectDeviceCount(devicename,companyid,companyids,portalType,medLineCodes,lineCode,beginTime,endTime);
            }
            catch (DataAccessException e)
            {
                _logger.error(ExceptionUtils.getStackTrace(e));
                throw new PwtzDatabaseException();
            }
            totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
        }else{
            pageNo=1;
            if(list!=null && list.size()>0){
                totalCount = list.size();
            }
        }
        paginationList.setTotalPage(totalPage);
        paginationList.setCurrPage(pageNo);
        paginationList.setPageSize(pageSize);
        paginationList.setRecords(list);
        paginationList.setTotalRecord(totalCount);
        return paginationList;
    }
    
    
	
}
